﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace game
{
    public abstract class Ship : RegularModelObject
    {
        public Squadron Squadron { get; protected set; }
        public Army Army { get; protected set; }
        public List<Weapon> Weapons { get; set; }

        public Ship Target { get; protected set; }

        public Shield Shield { get; protected set; }

        public int ActiveWeaponIndex { get; protected set; }

        public Weapon RocketLauncher { get; protected set; }

        protected float weaponCycleTimeMs;
        protected float timeFromLastCycleMs;

        public Ship(Scene scene, ModelUnit model, int layerIndex, Vector3 position, Squadron squadron)
            : base(scene, model, layerIndex)
        {
            this.position = position;
            this.Squadron = squadron;
            this.Army = squadron.Army;

            this.Weapons = new List<Weapon>();
            this.ActiveWeaponIndex = -1;

            this.weaponCycleTimeMs = 500f;
            this.timeFromLastCycleMs = 500f;

            this.Target = null;
        }

        public override void DrawPostProcess(GameTime gameTime, PostProcessEffect ppEffect)
        {
            if (ppEffect.Type == PostProcessEffectType.GLOW)
            {
                Glow glow = (Glow)ppEffect;
                glow.EP_gViewProj.SetValue(scene.ActiveCamera.View * scene.ActiveCamera.Projection);
                glow.EP_gTex.SetValue(model.GlowTexture);

                model.SetEffect(glow.ExtractEffect);

                foreach (ModelMesh mesh in model.Model.Meshes)
                {
                    glow.EP_gWorld.SetValue(model.transforms[mesh.ParentBone.Index] * World);
                    mesh.Draw();
                }

                model.SetDefaultEffects();
            }
        }

        #region commands

        protected void CycleWeaponLeft()
        {
            if (timeFromLastCycleMs > weaponCycleTimeMs)
            {
                timeFromLastCycleMs = 0f;

                ActiveWeaponIndex--;
                if (ActiveWeaponIndex < 0)
                    ActiveWeaponIndex = Weapons.Count - 1;
            }
        }

        protected void CycleWeaponRight()
        {
            if (timeFromLastCycleMs > weaponCycleTimeMs)
            {
                timeFromLastCycleMs = 0f;

                ActiveWeaponIndex++;
                if (ActiveWeaponIndex >= Weapons.Count)
                    ActiveWeaponIndex = 0;
            }
        }

        public override void Collision(GameObject obj)
        {
            if (Shield != null && Shield.On)
                Shield.Hit(obj.GetDamage(), obj.Position);
            else
                base.Collision(obj);
        }

        #endregion
    }
}
